/****************************************************************************
 *
 * Copyright (C) 2003-2010 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.bluecoat;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.StringTokenizer;
import nfse.NetFSERecord;
import nfse.gui.server.GUIRecordFactory;
import nfse.gui.server.RecordBuilderUtil;

public class BlueCoatRecordFactory extends GUIRecordFactory<BlueCoatRecord> {

    public BlueCoatRecordFactory() {
        builder = new RecordBuilderUtil();
        builder.addColumn(null, "ID", RecordBuilderUtil.BIGINT);
        RecordBuilderUtil.SQLField field = builder.addColumn("Start TS", "startts", RecordBuilderUtil.TIMESTAMP);
        field = builder.addColumn("Duration", "duration", RecordBuilderUtil.BIGINT);
        field = builder.addColumn("Src IP", "srcip", RecordBuilderUtil.INET);
        field = builder.addColumn("Dst IP", "dstip", RecordBuilderUtil.INET);
        field = builder.addColumn("Dst Host", "dsthost", RecordBuilderUtil.VARCHAR, 300);
        field = builder.addColumn("Src Bytes", "srcbytes", RecordBuilderUtil.BIGINT);
        field = builder.addColumn("Dst Bytes", "dstbytes", RecordBuilderUtil.BIGINT);
        field = builder.addColumn("Status", "status", RecordBuilderUtil.VARCHAR, 25);
        field = builder.addColumn("Action", "action", RecordBuilderUtil.VARCHAR, 25);
        field = builder.addColumn("Method", "method", RecordBuilderUtil.VARCHAR, 25);
        field = builder.addColumn("URI", "uri", RecordBuilderUtil.VARCHAR, 500);
        field = builder.addColumn("User Agent", "useragent", RecordBuilderUtil.VARCHAR, 500);
        field = builder.addColumn("Result", "result", RecordBuilderUtil.VARCHAR, 25);
        field = builder.addColumn("Category", "category", RecordBuilderUtil.VARCHAR, 100);
        this.typeName = "BlueCoat";
    }

    @Override
    protected void setValues(BlueCoatRecord record, int id) {
        int idx = -1;
        builder.setFieldValue(++idx, "" + id);
        builder.setFieldValue(++idx, "" + record.getStartTS());
        builder.setFieldValue(++idx, "" + record.duration);
        builder.setFieldValue(++idx, "" + record.getSrcAddr());
        builder.setFieldValue(++idx, "" + record.getDstAddr());
        builder.setFieldValue(++idx, "" + record.dstHost);
        builder.setFieldValue(++idx, "" + record.srcBytes);
        builder.setFieldValue(++idx, "" + record.dstBytes);
        builder.setFieldValue(++idx, "" + record.status);
        builder.setFieldValue(++idx, "" + record.action);
        builder.setFieldValue(++idx, "" + record.method);
        builder.setFieldValue(++idx, "" + record.uriStem);
        builder.setFieldValue(++idx, "" + record.userAgent);
        builder.setFieldValue(++idx, "" + record.filterResult);
        builder.setFieldValue(++idx, "" + record.filterCategory);
    }

    @Override
    public NetFSERecord generate(String line, int sensor) throws Exception {
        BlueCoatRecord rec = new BlueCoatRecord();
        if (line.charAt(0) == '#')
            return null;

        rec.setSensorID(sensor);
        rec.setInitialString(line);
        rec.setTypeID(this.typeID);
        rec.setTypeName(this.typeName);

        StringTokenizer st = new StringTokenizer(line, "\"", true);
        ArrayList<String> values = new ArrayList<String>(25);
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (token.equals("\"") && st.hasMoreTokens()) {
                values.add(st.nextToken());
                if (st.hasMoreTokens())
                    st.nextToken();
            } else if (!token.equals("\"")) {
                StringTokenizer st2 = new StringTokenizer(token);
                while (st2.hasMoreTokens()) {
                    values.add(st2.nextToken());
                }
            }

        }

        rec.setStartTS(Timestamp.valueOf(values.get(0) + " " + values.get(1)));
        rec.duration = Long.parseLong(values.get(2));
        rec.setSrcAddr(values.get(3));
        rec.status = values.get(4);
        rec.action = values.get(5);
        rec.dstBytes = Long.parseLong(values.get(6));
        rec.srcBytes = Long.parseLong(values.get(7));
        rec.method = values.get(8);
        rec.uriScheme = values.get(9);
        rec.dstHost = values.get(10);
        rec.proxyAddr = values.get(11); // ??
        rec.uriStem = values.get(12);
        rec.userName = values.get(13);
        rec.authGroup = values.get(14);
        rec.hierarchy = values.get(15);
        rec.dstAddr = values.get(16);
        rec.contentType = values.get(17);
        rec.userAgent = values.get(18);
        rec.filterResult = values.get(19);
        rec.filterCategory = values.get(20);
        rec.virusID = values.get(21);
        rec.proxyAddrAgain = values.get(22); // ??
        rec.siteName = values.get(23);
        rec.proxyPort = Integer.parseInt(values.get(24));
        return rec;
    }

    @Override
    public NetFSERecord generate() {
        return new BlueCoatRecord();
    }

}
